<?php 

namespace app\seller\controller;
use app\common\model\BaseGoods as BaseGoodsModel;
use app\seller\controller\Base;
use app\seller\validate\BaseGoodsValidate;
use app\seller\event\BaseGoodsEvent;
use app\common\utils\JsonService;
use think\facade\View;

class BaseGoods extends Base{


    /**
    * 编辑商品库表
    * @return mixed
    */
    public function update(){
        if($this->request->isAjax()){
            $post = $this->request->post();
            $validate = new BaseGoodsValidate;
            if(!$validate->scene('edit')->check($post)) {
                return JsonService::error($validate->getError());
            }
            return BaseGoodsEvent::updateBaseGoods($post);
        }

        $id = $this->request->get('id');
        $model = $this->findModel($id);
        return View::fetch('update',[
            'model'=>$model,
        ]);
    }

    /**
    * 商品库表列表

    * @return mixed
    */
    public function index(){
        $dataProvider = BaseGoodsEvent::baseGoodsList();
        return View::fetch('index',['dataProvider'=>$dataProvider]);
    }

    /**
    * 查看商品库表
    * @return mixed
    */
    public function view(){
        $id = $this->request->get('id');
        $model = $this->findModel($id);
        return View::fetch('view',[
            'model'=>$model,
        ]);
    }


    /**
    * 得到模型
    *
    * @param int $id
    * @param array $where
    *
    * @return array|false|\PDOStatement|string|\think\Model
    */
    public function findModel($id,$where = [])
    {
        if (empty($id) && empty($where)) {
            throw new \think\exception\RouteNotFoundException();
        }
        return BaseGoodsModel::findModel($id, $where);
    }

}
